#include "stdafx.h"
#include "ByteBuffer.h"
#include "ProtocolEnum.h"

CByteBuffer::CByteBuffer()
{
	m_pBuf = NULL;
	m_nBufLength = 0;
	m_nOffset = 0;
}

CByteBuffer::CByteBuffer(BYTE* pBuf, const int nBufLength)
{
	m_pBuf = pBuf;
	m_nBufLength = nBufLength;
	m_nOffset = 0;
}

CByteBuffer::~CByteBuffer()
{
	m_pBuf = NULL;
	m_nBufLength = 0;
	m_nOffset = 0;
}

bool CByteBuffer::ReadString(std::string& strRes)
{
	strRes = "";
	int nLength = 0;

	if (m_nBufLength - m_nOffset < sizeof(nLength))
		return false;
	
	if (!ReadInt(nLength))
		return false;
	if (m_nBufLength - m_nOffset < nLength)
		return false;

	strRes.append((char*)m_pBuf+m_nOffset, nLength);
	m_nOffset += nLength;

	return true;
}

bool  CByteBuffer::ReadByte(BYTE& nRes)
{
	nRes = 0;
	if (m_nBufLength - m_nOffset < sizeof(nRes))
		return false;

	memcpy_s(&nRes, sizeof(nRes), m_pBuf + m_nOffset, sizeof(nRes));
	m_nOffset += sizeof(nRes);

	return true;
}

bool CByteBuffer::ReadBoolean(bool& bRes)
{
	bRes = false;

	BYTE nValue = 0;
	if (!ReadByte(nValue))
		return false;

	if (nValue != 0)
		bRes = true;
	else
		bRes = false;

	return true;
}

bool CByteBuffer::ReadShort(short& nRes)
{
	nRes = 0;
	if (m_nBufLength - m_nOffset < sizeof(nRes))
		return false;

	memcpy_s(&nRes, sizeof(nRes), m_pBuf + m_nOffset, sizeof(nRes));
	m_nOffset += sizeof(nRes);

	return true;
}

bool CByteBuffer::ReadUShort(unsigned short &nRes)// WORD
{
	nRes = 0;
	if (m_nBufLength - m_nOffset < sizeof(nRes))
		return false;

	memcpy_s(&nRes, sizeof(nRes), m_pBuf + m_nOffset, sizeof(nRes));
	m_nOffset += sizeof(nRes);

	return true;
}

bool CByteBuffer::ReadInt(int& nRes)
{
	nRes = 0;
	if (m_nBufLength - m_nOffset < sizeof(nRes))
		return false;

	memcpy_s(&nRes, sizeof(nRes), m_pBuf + m_nOffset, sizeof(nRes));
	m_nOffset += sizeof(nRes);

	return true;
}

bool CByteBuffer::ReadUInt(unsigned int &nRes)// DWORD
{
	nRes = 0;
	if (m_nBufLength - m_nOffset < sizeof(nRes))
		return false;

	memcpy_s(&nRes, sizeof(nRes), m_pBuf + m_nOffset, sizeof(nRes));
	m_nOffset += sizeof(nRes);

	return true;
}

bool CByteBuffer::ReadInt64(INT64 &nRes)
{
	nRes = 0;
	if (m_nBufLength - m_nOffset < sizeof(nRes))
		return false;

	memcpy_s(&nRes, sizeof(nRes), m_pBuf + m_nOffset, sizeof(nRes));
	m_nOffset += sizeof(nRes);

	return true;
}

bool CByteBuffer::ReadUInt64(UINT64 &nRes)
{
	nRes = 0;
	if (m_nBufLength - m_nOffset < sizeof(nRes))
		return false;

	memcpy_s(&nRes, sizeof(nRes), m_pBuf + m_nOffset, sizeof(nRes));
	m_nOffset += sizeof(nRes);

	return true;
}

bool CByteBuffer::ReadFloat(float& fRes)
{
	fRes = 0.f;
	if (m_nBufLength - m_nOffset < sizeof(fRes))
		return false;

	memcpy_s(&fRes, sizeof(fRes), m_pBuf + m_nOffset, sizeof(fRes));
	m_nOffset += sizeof(fRes);

	return true;
}

bool CByteBuffer::ReadDouble(double& fRes)
{
	fRes = 0.f;
	if (m_nBufLength - m_nOffset < sizeof(fRes))
		return false;

	memcpy_s(&fRes, sizeof(fRes), m_pBuf + m_nOffset, sizeof(fRes));
	m_nOffset += sizeof(fRes);

	return true;
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


bool CByteBuffer::WriteString(const std::string& strValue)
{
	if (!WriteInt(strValue.length()))
		return false;

	if (m_nOffset + (int)strValue.length() > m_nBufLength)
		return false;

	memcpy_s(m_pBuf + m_nOffset, strValue.length(), strValue.c_str(), strValue.length());
	m_nOffset += strValue.length();

	return true;
}

bool CByteBuffer::WriteByte(const BYTE nValue)
{
	if (m_nOffset + (int)sizeof(nValue) > m_nBufLength)
		return false;

	memcpy_s(m_pBuf + m_nOffset, sizeof(nValue), &nValue, sizeof(nValue));
	m_nOffset += sizeof(nValue);

	return true;
}

bool CByteBuffer::WriteBoolean(const bool bValue)
{
	byte nValue = 0;
	if (bValue)
		nValue = 1;

	if (!WriteByte(nValue))
		return false;

	return true;
}

bool CByteBuffer::WriteShort(const short nValue)
{
	if (m_nOffset + (int)sizeof(nValue) > m_nBufLength)
		return false;

	memcpy_s(m_pBuf + m_nOffset, sizeof(nValue), &nValue, sizeof(nValue));
	m_nOffset += sizeof(nValue);

	return true;
}

bool CByteBuffer::WriteUShort(const unsigned short nValue)// WORD
{
	if (m_nOffset + (int)sizeof(nValue) > m_nBufLength)
		return false;

	memcpy_s(m_pBuf + m_nOffset, sizeof(nValue), &nValue, sizeof(nValue));
	m_nOffset += sizeof(nValue);

	return true;
}

bool CByteBuffer::WriteInt(const int nValue)
{
	if (m_nOffset + (int)sizeof(nValue) > m_nBufLength)
		return false;

	memcpy_s(m_pBuf + m_nOffset, sizeof(nValue), &nValue, sizeof(nValue));
	m_nOffset += sizeof(nValue);

	return true;
}

bool CByteBuffer::WriteUInt(const unsigned int nValue)// DWORD
{
	if (m_nOffset + (int)sizeof(nValue) > m_nBufLength)
		return false;

	memcpy_s(m_pBuf + m_nOffset, sizeof(nValue), &nValue, sizeof(nValue));
	m_nOffset += sizeof(nValue);

	return true;
}

bool CByteBuffer::WriteInt64(const INT64 nValue)
{
	if (m_nOffset + (int)sizeof(nValue) > m_nBufLength)
		return false;

	memcpy_s(m_pBuf + m_nOffset, sizeof(nValue), &nValue, sizeof(nValue));
	m_nOffset += sizeof(nValue);

	return true;
}

bool CByteBuffer::WriteUInt64(const UINT64 nValue)
{
	if (m_nOffset + (int)sizeof(nValue) > m_nBufLength)
		return false;

	memcpy_s(m_pBuf + m_nOffset, sizeof(nValue), &nValue, sizeof(nValue));
	m_nOffset += sizeof(nValue);

	return true;
}

bool CByteBuffer::WriteFloat(const float fValue)
{
	if (m_nOffset + (int)sizeof(fValue) > m_nBufLength)
		return false;

	memcpy_s(m_pBuf + m_nOffset, sizeof(fValue), &fValue, sizeof(fValue));
	m_nOffset += sizeof(fValue);

	return true;
}

bool CByteBuffer::WriteDouble(const double fValue)
{
	if (m_nOffset + (int)sizeof(fValue) > m_nBufLength)
		return false;

	memcpy_s(m_pBuf + m_nOffset, sizeof(fValue), &fValue, sizeof(fValue));
	m_nOffset += sizeof(fValue);

	return true;
}
